dofile("report.lua")

local HEADER =[[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
<title>Recorder</title>
<style type="text/css">
body { background:808080; }
table { border:outset 1px; }
td, th { border:inset 1px; }
td { background:A0A0A0; }
th { background:606060; }
.type-nil { color:red; }
.type-number { color:blue; }
.type-string { color:green; }
</style>
</head>
<body>
<h1>Recorder UNIT_NAME_UPDATE report</h1>
]]

local function escape(s)
	return s:gsub(".", function (c) return "\\"..string.byte(c) end)
end

local dofile = function (filename)
	local f = io.open(filename, "rb")
	local loader = function ()
		local line = f:read()
		if line then
			local before, content, after = line:match("^([%s+]\")(.*)(\", -- %[%d+%].*)")
			if before and content and after then
				content = escape(content)
				return before..content..after
			end
		end
		return line
	end
	local data = assert(load(loader, filename))
	data()
end

local function toliteral(value)
	local t, v = type(value)
	if t == "nil" then
		v = "nil"
	elseif t == "string" then
		v = string.format("%q", value):gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;")
	else
		v = tostring(value)
	end
	return string.format("<span class=\"type-%s\">%s</span>", t, v)
end

local function report_db(output, db)
	output("<table>\n")
	output("<tr>")
	output("<th>unit</th>")
	output("<th>count</th>")
	output("<th>start_id</th>")
	output("<th>end_id</th>")
	output("<th>start_time</th>")
	output("<th>end_time</th>")
	output("</tr>\n")
	
	local unu = {}
	
	for i = 1, #db.events do
		local event = db.events[i]
		if event == "UNIT_NAME_UPDATE" then
			local offset = db.offsets[i]
			local unit = db.args[offset]
			local time = db.times[i] - db.times[1]
			
			local r = unu[unit]
			if not r then
				r = {
					count = 1,
					start_id = i,
					start_time = time,
					end_id = i,
					end_time = time,
				}
				unu[unit] = r
			else
				r.count, r.end_id, r.end_time = r.count + 1, i, time
			end
		end
	end
	for unit, values in pairs(unu) do
		output("<tr>")
		output("<td>%s</td>", toliteral(unit))
		output("<td>%s</td>", toliteral(values.count))
		output("<td>%s</td>", toliteral(values.start_id))
		output("<td>%s</td>", toliteral(values.end_id))
		output("<td>%s</td>", toliteral(values.start_time))
		output("<td>%s</td>", toliteral(values.end_time))
		output("<tr>")
	end
	output("</table>\n")
end

local function main()
	dofile("!!!Recorder3.lua")
	local f = io.open("report_unu.html", "w")
	local output = function (...)
		f:write((string.format(...)))
	end
	output(HEADER)
	output("<h1>Startup Log</h1>")
	report_db(output, RecorderDB)
	if CheckLockDB then
		output("<h1>Lockup Logs</h1>")
		for index, db in ipairs(CheckLockDB) do
			output("<h2>Lockup #%d (%.1f seconds)</h2>", index, db.lockup)
			report_db(output, db)
		end
	end
	output("</body>\n")
	output("</html>\n")
end

main()
